﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using msBsc.AppLogic.DataAlc;
using System.Data;

namespace msBsc.WebUi.Custom
{
    public partial class wfRegUserInfoFill : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string stremkey=this.GetFormParam("KEY");
                if(stremkey==string.Empty)
                {
                    //this.ExecJScript("window.location.href = '/c_RegMailSend.htm';");
                    return;
                }
                stremkey=msBsc.AppLogic.Cfg.DesCry.uncMe(stremkey,"rmdes");
                string[] arrten=stremkey.Split(',');
                if(arrten.Length !=2)
                {
                    //this.ExecJScript("window.location.href = '/c_RegMailSend.htm';");
                    return;
                }
                DateTime dtreg=DateTime.Now.AddDays(-2);
                if(!DateTime.TryParse(arrten[1],out  dtreg) || dtreg<DateTime.Now.AddDays(-1))
                {
                    this.ExecJScript("reRegister();");
                    //this.ExecJScript("window.location.href = '/c_RegMailSend.htm';");
                    return;
                }
                this.ExecJScript("$('#spnMailNme').html('" + arrten[0] + "')");
                this.ExecJScript("LS.set('CURR_REG_MAIL','" + arrten[0] + "');");
                this.ExecJScript("if(LS.get('" + arrten[0] + "') && LS.get('" + arrten[0] + "') != 'STEP3'){ LS.set('" + arrten[0] + "','STEP2');}");
                this.ExecJScript("LS.set('" + arrten[0] + "_RDT','" + DateTime.Now.ToString() + "');");
                this.ExecJScript("validateStep();");
            }
        }
        public override void DoAction()
        {
            if (this.strAction == "REGIST_STEP2")
            {
                if (this.GetParamHtValue("EMAIL") != string.Empty
                    && this.GetParamHtValue("PASS1") != string.Empty
                    && this.GetParamHtValue("PASS2") != string.Empty
                    && this.GetParamHtValue("USER_NME") != string.Empty
                    && this.GetParamHtValue("PASS1") == this.GetParamHtValue("PASS2"))
                {

                    using (CBizSysUser objSysUser = new CBizSysUser())
                    {
                        using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
                        {
                            System.Data.DataSet ds1 = oCDbAccess.QueryBySQL("SELECT TOP 1 E_MAIL FROM SYS_USER WHERE E_MAIL='" + this.GetParamHtValue("EMAIL") + "'");
                            if (ds1.Tables[0].Rows.Count > 0)
                            { 
                                this.Alert("您输入的邮箱地址已被注册，请重新选择邮箱注册！");
                                return;
                            }
                            if (this.GetParamHtValue("PROMOTION_NME") != string.Empty)
                            {
                                System.Data.DataSet ds2 = oCDbAccess.QueryBySQL("SELECT TOP 1 E_MAIL FROM SYS_USER WHERE USER_TYPE_CID='3' AND USER_NME='" + this.GetParamHtValue("PROMOTION_NME") + "'");
                                if (ds2.Tables[0].Rows.Count <= 0)
                                {
                                    this.Alert("您输入的推广员不存在，请重新输入！");
                                    this.ExecJScript("$('#txtPROMOTION_NME').focus();");
                                    return;
                                }
                            }
                        }
                        DataSet ds = objSysUser.SelectByNameOrEmail(this.GetParamHtValue("USER_NME"), "");
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            this.Alert("会员名已存在，不能重复，请重新录入！");
                            this.ExecJScript("$('#J_Nick').focus();");
                            return;
                        }
                        DataRow drNew = ds.Tables[0].NewRow();
                        drNew["USER_NME"] = this.GetParamHtValue("USER_NME");
                        drNew["LOGIN_PSWD"] = this.GetParamHtValue("PASS1");
                        drNew["E_MAIL"] = this.GetParamHtValue("EMAIL");
                        drNew["TEL"] = this.GetParamHtValue("USER_TEL");
                        drNew["QQ_NO"] = this.GetParamHtValue("QQ_NO");
                        drNew["PROMOTION_NME"] = this.GetParamHtValue("PROMOTION_NME");
                        drNew["COMPANY_NME"] = "";
                        drNew["OFCE_NME"] = "";
                        drNew["USER_TYPE_CID"] = "2";//0系统用户，1商户，2客户
                        drNew["REC_ACTIVE"] = true;

                        ds.Tables[0].Rows.Add(drNew);

                        if (ds.HasChanges())
                        {
                            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                            transactionOption.Timeout = new TimeSpan(0, 10, 0);
                            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                            {
                                objSysUser.Update(ds);
                                ds.AcceptChanges();
                                ts.Complete();
                                ts.Dispose();
                            }
                        }
                    }

                    //msBsc.AppLogic.Cfg.UserInfo ouserinfo = new AppLogic.Cfg.UserInfo();
                    msBsc.SvRef.UserInfo.Login(this.GetParamHtValue("USER_NME"), this.GetParamHtValue("PASS1"));
                    this.ExecJScript("LS.set('CURR_REG_MAIL','" + this.GetParamHtValue("EMAIL") + "');");
                    this.ExecJScript("LS.set('" + this.GetParamHtValue("EMAIL") + "','STEP3');");
                    this.ExecJScript("LS.set('" + this.GetParamHtValue("EMAIL") + "_RDT','" + DateTime.Now.ToString() + "');");
                    this.ExecJScript("window.location.href = '/c_RegSuccess.htm';");
                     
                }
            }
        }
    }
}